//
//  LNHConfirmOrderHeaderView.m
//  LNH
//
//  Created by cruise on 2018/2/15.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "LNHConfirmOrderHeaderView.h"
#import "LNHConfirmOrderViewModel.h"

@interface LNHConfirmOrderHeaderView()

@property (nonatomic, strong) UIImageView *locationImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, strong) UIView *lineView;


@end

@implementation LNHConfirmOrderHeaderView

/**
 为视图绑定 viewModel
 
 @param viewModel 要绑定的ViewModel
 @param params 额外参数
 */
- (void)bindViewModel:(id <LMViewProtocol>)viewModel withParams:(NSDictionary *)params{
    if ([viewModel isKindOfClass:[LNHConfirmOrderViewModel class]]){
        
        LNHConfirmOrderViewModel *_viewModel = (LNHConfirmOrderViewModel *)viewModel;
        
        [[RACObserve(_viewModel, addressDict) filter:^BOOL(id  _Nullable value) {
            return value != nil;
        }] subscribeNext:^(NSDictionary *dict) {
            
            [self.nameLabel setText:dict[@"name"]];
            if (IsStrEmpty(dict[@"province_name"])) {
                [self.addressLabel setText:@"请选择收货地址"];
            } else {
                [self.addressLabel setText:[NSString stringWithFormat:@"%@%@%@%@", dict[@"province_name"], dict[@"city_name"], dict[@"area_name"], dict[@"address"]]];
            }
            
        }];
        
    }
}

/**
 初始化额外数据
 */
- (void)lm_initializeForView{
    
}

/**
 初始化视图
 */
- (void)lm_createViewForView{
    [self addSubview:self.locationImageView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.addressLabel];
    [self addSubview:self.arrowImageView];
    [self addSubview:self.lineView];

    [self setNeedsUpdateConstraints];
}

-(void) updateConstraints {
    
    [self.locationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(10);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.locationImageView.mas_top);
        make.left.equalTo(self.locationImageView.mas_right).offset(5);
        make.right.equalTo(self.arrowImageView.mas_left).offset(-10);
        make.height.mas_equalTo(20);
    }];
    
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom);
        make.left.equalTo(self.nameLabel);
        make.right.equalTo(self.nameLabel);
        make.height.mas_equalTo(20);
    }];
    

    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.right.equalTo(self).offset(-20);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.mas_equalTo(2);
    }];
    
    [super updateConstraints];
}

#pragma mark - getter

-(UIImageView *) locationImageView {
    if (!_locationImageView) {
        _locationImageView = [UIImageView new];
        [_locationImageView setImage:LMImageNamed(@"confirm_order_address_bottom_img")];
        [_locationImageView setContentMode:UIViewContentModeCenter];
    }
    return _locationImageView;
}

-(UIImageView *) arrowImageView {
    if (!_arrowImageView) {
        _arrowImageView = [UIImageView new];
        [_arrowImageView setImage:LMImageNamed(@"right_arrow_img")];
        [_arrowImageView setContentMode:UIViewContentModeCenter];
    }
    return _arrowImageView;
}

-(UILabel *) nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        [_nameLabel setText:@"收货人：美少女"];
        [_nameLabel setTextColor:RGBCOLOR(62,58,57)];
        [_nameLabel setFont:[UIFont systemFontOfSize:14]];
    }
    return _nameLabel;
}

-(UILabel *) addressLabel {
    if (!_addressLabel) {
        _addressLabel = [UILabel new];
        [_addressLabel setText:@"收货地址：广东省广州市白云区绿地中心1407"];
        [_addressLabel setTextColor:RGBCOLOR(62,58,57)];
        [_addressLabel setFont:[UIFont systemFontOfSize:14]];
    }
    return _addressLabel;
}

-(UIView *) lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        [_lineView setBackgroundColor:RGBCOLOR(233, 85, 19)];
    }
    return _lineView;
}

@end
